package com.kehutong.payfees.service;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import com.kehutong.payfees.entity.ChargeBillLog;
import com.kehutong.payfees.enums.BillOperateType;
import org.coraframework.util.Objects;


/**
 * Author:  peter
 * Date:    2020/7/10
 * Function:缴费模块相关日志记录
 */
@WebService("/payfees/charge/log")
public class ChargeLogServiceImpl {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    protected JdbcSession jdbcSession;


    public void saveLog(String billNo, BillOperateType type, JSONObject req, String operatorName, String operatorId) {
        saveLog(billNo, type, null, req, operatorName, operatorId);
    }

    public void saveLog(String billNo, BillOperateType type, String remark, JSONObject req, String operatorName, String operatorId) {
        ChargeBillLog log = Pool.newInstance(ChargeBillLog.class);

        log.setBillNo(billNo);
        log.setType(type);

        if (Objects.nonEmpty(req)) {
            req.remove("token");
            log.setParams(req.toJSONString());
        }
        log.setRemark(remark);

        log.setOperatorName(operatorName);
        log.setOperatorId(operatorId);

        jdbcSession.insert(log);
    }

    public void saveSystemLog(String billNo, BillOperateType type, String remark) {

        ChargeBillLog log = Pool.newInstance(ChargeBillLog.class);

        log.setBillNo(billNo);
        log.setType(type);

        log.setOperatorName("system");
        log.setRemark(remark);

        jdbcSession.insert(log);
    }

}
